package com.hellobike.haxing.web.shop;

import com.hellobike.haxing.order.api.IOrderStatsService;
import com.hellobike.haxing.order.model.request.ProductStatRequest;
import com.hellobike.haxing.order.model.request.ShopDistributionPageRequest;
import com.hellobike.haxing.order.model.vo.DistributionStatisVo;
import com.hellobike.haxing.order.model.vo.ProductStatDetailResult;
import com.hellobike.haxing.order.model.vo.ShopDistributionStatsVo;
import com.hellobike.haxing.order.result.PageResult;
import com.hellobike.haxing.order.result.Result;
import com.hellobike.haxing.response.distribution.ShopStatDetailRequest;
import com.hellobike.haxing.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping(value = "/distribution")
@Api(tags = "分销统计数据")
public class DistributionController {


    @Reference(version = "1.0.0")
    private IOrderStatsService orderStatsService;


    @Autowired
    private UserUtil userUtil;

    @ApiOperation("我的分销统d计数据")
    @PostMapping(value = "/stats")
    public Result<DistributionStatisVo> distributionStats() {
        DistributionStatisVo distributionStatisVo = orderStatsService.distributionStatisVo(userUtil.getUser().getShopVo().getId());
        return Result.ok(distributionStatisVo);
    }

    @ApiOperation("我发展的店铺信息")
    @PostMapping(value = "/shop-stats")
    public PageResult<ShopDistributionStatsVo> shopDistributionStats(@RequestBody ShopDistributionPageRequest request) {
        request.setShopId(userUtil.getUser().getShopVo().getId());
        return orderStatsService.flowerList(request);
    }


    @PostMapping(value = "/shop-commission-detail")
    @ApiOperation("我的提成ds")
    public ProductStatDetailResult shopStatsDetail(@RequestBody ShopStatDetailRequest request) {
        ProductStatRequest productStatRequest = new ProductStatRequest();
        productStatRequest.setShopId(userUtil.getUser().getShopVo().getId());
        productStatRequest.setTimestamp(request.getTimestamp());
        LocalDateTime now = LocalDateTime.now();
        productStatRequest.setEndDate(now.plusDays(1));
        if (request.getType() == 1) {
            productStatRequest.setStartDate(now.withHour(0).withMinute(0).withSecond(0));
            productStatRequest.setEndDate(now.plusDays(1));
        } else if (request.getType() == 2) {
            productStatRequest.setStartDate(now.minusDays(8).withHour(0).withMinute(0).withSecond(0));
            productStatRequest.setEndDate(now.plusDays(1));
        } else {
            productStatRequest.setStartDate(LocalDateTime.of(2000, 1, 1, 0, 0, 0));
        }
        productStatRequest.setCurPage(request.getCurPage());
        productStatRequest.setPageSize(request.getPageSize());
        return orderStatsService.detail(productStatRequest);
    }

}
